using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantPlantTipSpring
/// \brief  Plant tip spring that make the tip return in its initial position
[AddComponentMenu("GK/AbyssalGiant/Plant/gkAbyssalGiantPlantTipSpring")]
public class gkAbyssalGiantPlantTipSpring : MonoBehaviour 
{
	/// The spring stiffness 
	public float Stiffness = 20.0f;
	
	/// The Physic component
	private gkPhysicComponent m_rPhysicComponent;
	
	/// The initial local position
	private Vector3 m_f3InitialLocalPosition;
	
	/// \brief  Called just after the game begin to play
    void Start()
    {
        // Get the physic component
        m_rPhysicComponent = GetComponent<gkPhysicComponent>();
		
		// Save the initial local position
		m_f3InitialLocalPosition = transform.localPosition;
	}
	
	/// \brief  Update is called once per frame
	void LateUpdate()
	{
		Vector3 f3CurrentPositionToInitialPosition;
		
		// Comppute the distance between the initial and the current position
		f3CurrentPositionToInitialPosition = Stiffness * (m_f3InitialLocalPosition - transform.localPosition);
		f3CurrentPositionToInitialPosition = transform.TransformDirection(f3CurrentPositionToInitialPosition);
		
		m_rPhysicComponent.AddForce(f3CurrentPositionToInitialPosition);
	}
}